package com.mark.common.utils;

import com.google.common.collect.Lists;
import com.mark.common.exception.Campuso2oException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;

/**
 * @Description: 配置文件处理工具类
 * @Author: Mark
 * @CreateDate: 2018/3/26.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Slf4j
public class PropertiesUtil {
    private static Properties properties;
    private static Properties dataSourceProperties;
    public static  final String  FTP_SERVER_HTTP_PREFIX =  "http://img.mark.com/";

    static {//在静态代码块中读取配置问价
        String fileName = "campus.properties";
        String dataSourceFileName = "dataSource.properties";
        properties  = new Properties();
        dataSourceProperties = new Properties();
        try {
            properties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName),"UTF-8"));
            dataSourceProperties.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(dataSourceFileName),"UTF-8"));
        } catch (IOException e) {
            log.error("PropertiesUtil中读取配置文件异常："+e.getMessage());
        }catch (Exception e){
            log.error("PropertiesUtil中读取配置文件出现了其他的异常："+e.getMessage());
        }
    }

    public static String getProperty(String key){
        String value = properties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();
    }

    public static Integer getIntegerProperty(String key){
        String value = getProperty(key);
        if(StringUtils.isBlank(value)){
            throw new Campuso2oException("读取配置文件中的"+key+"的值为空！");
        }
        return Integer.parseInt(value.trim());
    }

    public static Integer getIntegerProperty(String key,Integer defaultValue){
        String value = getProperty(key);
        if(StringUtils.isBlank(value)){
            return defaultValue;
        }
        return Integer.parseInt(value.trim());
    }


    public static String getProperty(String key,String defaultValue){
        String value = properties.getProperty(key.trim());
        if(StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }

    /**
     * 读取数据库配置文件信息
     * @param key
     * @return
     */
    public static String getDataSourceProperty(String key){
        String value = dataSourceProperties.getProperty(key);
        if (StringUtils.isBlank(value)){
            return null;
        }
        return value.trim();
    }

    public static String getDataSourceProperty(String key,String defaultValue){
        String value = dataSourceProperties.getProperty(key);
        if (StringUtils.isBlank(value)){
            value = defaultValue;
        }
        return value.trim();
    }

    /**
     * 将以逗号分隔的整数字符转换为整型集合
     * propertyValue：'3,11,10'
     * result：[3,11,10]
     *
     * @param propertyValue 以逗号分隔的整数字符,该值定义在配置文件中，
     *
     */
    public static List<Integer> getIntegerListProperty(String propertyValue){
        final String split = ",";
        return getIntegerListProperty(propertyValue, split);
    }

    /**
     * 将以指定字符split分隔的整数字符转换为整型集合
     * propertyValue：'3,11,10'
     * result：[3,11,10]
     *
     * @param propertyValue 以逗号分隔的整数字符,该值定义在配置文件中，
     *
     * @param split 字符propertyValue中的分隔符
     */
    public static List<Integer> getIntegerListProperty(String propertyValue,String split){
        //参数校验
        if(StringUtils.isBlank(propertyValue)){
            throw new Campuso2oException("输入的配置文件的参数值不能空！");
        }
        if(StringUtils.isBlank(split)){
            throw new Campuso2oException("输入的分隔不为空！");
        }
        String[] properties = propertyValue.split(split);
        List<Integer> result = Lists.newArrayList();
        for(String property : properties){
            int value = Integer.parseInt(property);
            result.add(value);
        }
        return result;
    }


}
